package cn.lizemin.encrypt;

import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.util.Zip4jConstants;

import java.io.File;


/**
 * @author: lizemin
 * @version: 1.0
 */
public class ZipTool {
    //这是一个main方法,是程序的入口
    public static void main(String[] args) {
        encrypt_zip("C:\\Users\\15871\\Desktop\\mybatis\\20210703T155605313.xls",
                "C:\\Users\\15871\\Desktop\\mybatis\\20210703T155605313.zip",
                "12345");

    }

    public static void encrypt_zip(String src_file, String dst_file, String encode) {
        File file = new File(src_file);

        ZipParameters parameters = new ZipParameters();
        parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);//压缩方式
        parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); // 压缩级别

        parameters.setEncryptFiles(true);
        parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD);//加密方式
        parameters.setPassword(encode.toCharArray());//设置密码

        try {
            ZipFile zipFile = new ZipFile(dst_file);
            zipFile.setFileNameCharset("gbk");
            zipFile.addFile(file, parameters);

        } catch (ZipException e) {
            e.printStackTrace();
        }

    }

}
